package com.disc.controller;

import com.disc.baidu.BaiduUser;
import com.disc.common.Constant;
import com.disc.common.DiscResponse;
import com.disc.service.DiscService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

@Controller
@RequestMapping("/manage")
public class ManageController {

    @Autowired
    DiscService discServiceImpl;

    @RequestMapping("index")
    public ModelAndView gotoIndexPage(HttpSession session)
    {
        ModelAndView modelAndView = new ModelAndView("admin/index");
        modelAndView.addObject("user",session.getAttribute("user"));
        modelAndView.addObject("panList",Constant.cookieStoreMap);
        modelAndView.addObject("panListSize",Constant.cookieStoreMap.size());
        return modelAndView;
    }

    @RequestMapping("video")
    public ModelAndView gotoVideoPage()
    {
        return new ModelAndView("video");
    }

    @RequestMapping("gotoTianYiLogin")
    public ModelAndView gotoTianYiLogin()
    {
        return new ModelAndView("admin/tianyilogin");
    }

    @RequestMapping("gotoHeikeLogin")
    public ModelAndView gotoHeikeLogin(HttpSession session)
    {
        discServiceImpl.initHeiKeCookie(session);
        return new ModelAndView("admin/heikeLogin");
    }

    @RequestMapping("getVcodeImageStream")
    public void getVcodeImageStream(HttpSession session,HttpServletResponse response)
    {
        try
        {
            byte[] vcodeImage = discServiceImpl.getVcodeImageStream(session);
            OutputStream stream = response.getOutputStream();
            response.setContentType("image/gif");
            stream.write(vcodeImage);
            stream.flush();
            stream.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 黑科登录
     * @param baiduUser
     * @param session
     * @return
     */
    @RequestMapping("heikeLogin")
    @ResponseBody
    public DiscResponse heikeLogin(BaiduUser baiduUser, HttpSession session)
    {
        return discServiceImpl.loginHeiKe(baiduUser,session);
    }

    @RequestMapping("gotoPanListPage")
    public ModelAndView gotoPanListPage(HttpSession session)
    {
        ModelAndView modelAndView = new ModelAndView("admin/pan-list");
        modelAndView.addObject("panList",Constant.cookieStoreMap);
        modelAndView.addObject("user",session.getAttribute("user"));
        return modelAndView;
    }
}
